package com.wxxymakers.grademark.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxxymakers.grademark.exception.ServiceException;
import com.wxxymakers.grademark.model.entity.WxDepartment;
import com.wxxymakers.grademark.model.mapper.WxDepartmentMapper;
import com.wxxymakers.grademark.model.mapper.WxUserMapper;
import com.wxxymakers.grademark.utils.NumUtils;
import com.wxxymakers.grademark.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @AUTHOR soft
 * @DATE 2018/12/21 15:08
 * @DESCRIBE 部门
 */
@Service
public class WxDepartmentService {

    @Autowired
    private WxUserMapper userMapper;

    @Autowired
    private WxDepartmentMapper departmentMapper;

    @Transactional
    public boolean add(WxDepartment department) {
        if (NumUtils.lteZero(department.getCollageId())) {
            department.setCollageId(1);
        }

        String name = department.getName();
        if (byNameAndCollage(name, department.getCollageId()) != null) {
            throw new ServiceException("部门已存在，请勿重复添加！");
        }
        department.setDatetime(new Date());
        return departmentMapper.insert(department) > 0;
    }

    @Transactional
    public boolean delete(int id) {
        if (countPeopleWithDept(id) > 0) {
            throw new ServiceException("该部门还有不少人呢，别删哦！");
        }
        return departmentMapper.deleteById(id) > 0;
    }

    @Transactional
    public boolean change(WxDepartment department) {
        if (department.getId() == null || department.getId() <= 0) {
            throw new ServiceException("无法修改，缺失部门必要信息！");
        }

        String name = department.getName();
        WxDepartment dept = byNameAndCollage(name, department.getCollageId());
        if (dept != null && !dept.getId().equals(department.getId())) {
            throw new ServiceException("部门已存在，无法完成修改！");
        }
        return departmentMapper.updateById(department) > 0;
    }

    public WxDepartment byNameAndCollage(String name, Integer collageId) {
        WxDepartment department = new WxDepartment();
        department.setName(name);
        department.setCollageId(collageId);
        QueryWrapper<WxDepartment> wrapper = new QueryWrapper<>(department);
        List<WxDepartment> wxDepartments = departmentMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(wxDepartments)) {
            return wxDepartments.get(0);
        }
        return null;
    }

    public WxDepartment byId(int id) {
        return departmentMapper.selectById(id);
    }

    /**
     * 根据学院id查询所有部门
     * @param collageId 学院id
     */
    public List<WxDepartment> list(Integer collageId) {
        QueryWrapper<WxDepartment> wrapper = new QueryWrapper<>();
        if (NumUtils.gtZero(collageId)) {
            wrapper.eq("collage_id", collageId);
        }
        return departmentMapper.selectList(wrapper);
    }

    /**
     * 获取部门下的人数
     * @param deptId 部门id
     */
    public long countPeopleWithDept(int deptId) {
        return userMapper.countByDept(deptId);
    }

    public Page<WxDepartment> page(int page, int limit, WxDepartment query) {
        QueryWrapper<WxDepartment> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        if (query != null) {
            if (!StringUtils.isEmpty(query.getName())) wrapper.like("name",  query.getName());
            if (NumUtils.gtZero(query.getCollageId())) wrapper.eq("collage_id", query.getCollageId());
        }

        int total = departmentMapper.selectCount(wrapper);
        if (total > 0) {
            List<WxDepartment> wxDepartments = departmentMapper.selectPage(PageUtils.rowBounds(page, limit), wrapper)
                    .getRecords();
            return PageUtils.page(wxDepartments, page, limit, total);
        }
        return null;
    }
}
